package searchteam.client.ui.widgets;

import java.util.ArrayList;

import searchteam.client.beansMirrors.TeamMirror;
import searchteam.client.services.SearchTeamServicesAsync;
import searchteam.client.ui.SearchTeamUI;
import searchteam.client.ui.callbacks.createTeamCallback;
import searchteam.client.ui.callbacks.getAllPersonsCallback;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

public class CreateTeamPopUp extends DialogBox implements Display {
	
	private TeamMirror team;
	private SearchTeamServicesAsync services;
	private DataPanel panel;
	private ArrayList<MirrorDisplay> persons;
	private ListBox personsLB;
	
	public CreateTeamPopUp(DataPanel p, SearchTeamServicesAsync s) {
		team = null;
		services = s;
		panel = p;
		persons = new ArrayList<MirrorDisplay>();
		
		Grid mainPanel = new Grid(5, 2);
		
		this.setTitle("Creation d'Equipe");
		this.setText("Creation d'Equipe");
		this.setPopupPosition(100, 100);
		
		final TextBox nameTB = new TextBox();
		final TextBox domainTB = new TextBox();
		final TextBox statusTB = new TextBox();
		personsLB = new ListBox();
		
		personsLB.setVisibleItemCount(1);
		services.getAllPersons(new getAllPersonsCallback(this));
		
		Button buttonOK = new Button("enregistrer");
		Button buttonKO = new Button("annuler");
		
		buttonOK.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				team = new TeamMirror(nameTB.getText(), domainTB.getText(), statusTB.getText(), ((PersonMirrorDisplay)persons.get(personsLB.getSelectedIndex())).getBean());
				services.createTeam(team, new createTeamCallback(panel));
				CreateTeamPopUp.this.hide();
			}
		});
		
		buttonKO.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				CreateTeamPopUp.this.hide();
			}
		});
		
		mainPanel.setWidget(0, 0, new Label("Nom"));
		mainPanel.setWidget(0, 1, nameTB);
		mainPanel.setWidget(1, 0, new Label("Domaine"));
		mainPanel.setWidget(1, 1, domainTB);
		mainPanel.setWidget(2, 0, new Label("Statut"));
		mainPanel.setWidget(2, 1, statusTB);
		mainPanel.setWidget(3, 0, new Label("Chef"));
		mainPanel.setWidget(3, 1, personsLB);
		mainPanel.setWidget(4, 0, buttonOK);
		mainPanel.setWidget(4, 1, buttonKO);
		
		this.add(mainPanel);
		this.show();
	}

	public void addMirror(MirrorDisplay mirror) {
		personsLB.addItem(mirror.getText());
		persons.add(mirror);
	}

	public void clearMirrors() {
		personsLB.clear();
		persons.clear();
	}

	public SearchTeamUI getMainUI() {
		return panel.getMainUI();
	}

	public void refresh() {
		// TODO Auto-generated method stub
		
	}
}
